﻿using ConfExpert.Forms.Base;
using DevExpress.XtraEditors.Repository;
using DevTeam;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class RaporBoyu : XForm
    {
        public RaporBoyu()
        {
            InitializeComponent();
        }

        int ProductId = 0;
        private void RaporBoyu_Load(object sender, EventArgs e)
        {
            
            Fetch();
            btn.ItemClick += btn_ItemClick;

            pictureEdit1.MouseMove += new MouseEventHandler(pictureEdit1_MouseMove);
            pictureEdit1.MouseUp += new MouseEventHandler(pictureEdit1_MouseUp);
            pictureEdit1.MouseDown += new MouseEventHandler(pictureEdit1_MouseDown);
            //pictureEdit1.Properties.ShowZoomSubMenu = DevExpress.Utils.DefaultBoolean.True;

            ((RepositoryItemZoomTrackBar)zoomTrackBarControl1.Edit).Minimum = -5;
            ((RepositoryItemZoomTrackBar)zoomTrackBarControl1.Edit).Maximum = 5;
            zoomTrackBarControl1.EditValue = 0;
            zoomTrackBarControl1.EditValueChanged += zoomTrackBarControl1_EditValueChanged;

            pictureEdit1.Cursor = Cursors.Cross;
            PenBlack = new Pen(Color.Black, 2);
            PenBlack.DashStyle = DashStyle.Solid;

            ((RepositoryItemColorPickEdit)colorPickEdit1.Edit).ColorChanged += colorPickEdit1_ColorChanged;
            colorPickEdit1.EditValue = Color.Black;

            gle.Edit.Assign(RI(Rp.V_Kumas));
            gle.EditValueChanged += gle_EditValueChanged;
        }

        void btn_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Fetch();
        }

        void gle_EditValueChanged(object sender, EventArgs e)
        {
            ProductId = (int)gle.EditValue;
        }

        void colorPickEdit1_ColorChanged(object sender, EventArgs e)
        {
            PenBlack.Color = (Color)colorPickEdit1.EditValue;
        }


        void zoomTrackBarControl1_EditValueChanged(object sender, EventArgs e)
        {
            int val = zoomTrackBarControl1.EditValue.ToInt();
            if (val > 0)
                pictureEdit1.Properties.ZoomPercent = 100 * (val + 1);
            else if (val < 0)
                pictureEdit1.Properties.ZoomPercent = 100 / (-val + 1);
            else
                pictureEdit1.Properties.ZoomPercent = 100;
        }

        int _cropX, _cropY;
        Rectangle _selection;
        Pen PenBlack;
        bool _drawStarted = false;
        private void pictureEdit1_MouseDown(object sender, MouseEventArgs e)
        {
            if (_drawStarted == false)
            {
                _drawStarted = true;

                if (e.Button == System.Windows.Forms.MouseButtons.Left)
                {
                    _cropX = e.X;
                    _cropY = e.Y;
                    _selection = new Rectangle(new Point(e.X, e.Y), new Size());
                }

                pictureEdit1.Refresh();
            }
        }

        private void pictureEdit1_MouseMove(object sender, MouseEventArgs e)
        {

            if (pictureEdit1.Image == null)
                return;

            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                pictureEdit1.Refresh();
                _selection.Width = e.X - _selection.X;
                _selection.Height = e.Y - _selection.Y;
                pictureEdit1.CreateGraphics().DrawRectangle(PenBlack, _cropX, _cropY, _selection.Width, _selection.Height);
            }
        }

        private void pictureEdit1_MouseUp(object sender, MouseEventArgs e)
        {

            if (e.Button == MouseButtons.Left && _drawStarted && _selection.Size != new Size())
            {
                decimal zp = (decimal)pictureEdit1.Properties.ZoomPercent;
                int w = Convert.ToInt32(_selection.Width * 100 / zp);    // * (500 pixel gerçekte kaç mm.)
                int h = Convert.ToInt32(_selection.Height * 100 / zp);    // * (500 pixel gerçekte kaç mm.)
                Notify(w.ToString() + " " + h.ToString());

                //TODO:
                //butonları resmin altına koy
                //cross cursor çıksın pictureedit in içinde
                //rapor boyu tek yönde mi iki yönde mi? tek yönde ise büyüğü/küçüğü??
                //w mi h mi dikkate alınacak? iki tane buton mu olsun içindeki rakam güncellenen?
                //resim bozuk butonu (ve niye bozuk olduğunu gösteren lookup)
                //update.. product tablosundaki yeni bir alan mı olmalı??


                /*
                _croppedImage = (_displayedImage as Bitmap).Clone(_selection, _displayedImage.PixelFormat);

                modifiedPictureEdit.Image = _croppedImage;
                modifiedPictureEdit.Width = _croppedImage.Width;
                modifiedPictureEdit.Height = _croppedImage.Height;
                 */
            }

            //Cursor = Cursors.Default;

            _drawStarted = false;
        }
        void Fetch()
        {
            
            ds.Clear();
            GetDetails("xProductPhotos;xBuffer", "GetProductPhoto", ProductId, 500);
            if (ds.xProductPhotos.Rows.Count > 0)
                if (!ds.xProductPhotos[0].IsPhotoNull())
                {
                    ProductId = ds.xProductPhotos[0].ProductId;
                    pictureEdit1.EditValue = ds.xProductPhotos.FindByProductId(ProductId).Photo;
                    gle.EditValue = ds.xBuffer[0].intVal;
                    //txtInfo.Caption = ds.xBuffer[0].strVal;
                }
        }



    }
}
